# ------------------- Frontiers paper ------------------------ #
#   Module:             Data construction and analysis    
#   Specifically:       Helper functions
#   Author:             Karsten, Mirko and Nina
# ------------------------------------------------------------ #

#' Fix stargazer empty output
#' @seealso https://www.reddit.com/r/rstats/comments/5hcmbm/stargazer_and_gsheet_outputs_empty_tables_a/
stargaze.it <- function(x,y,o) {
  write.csv(x, file = "stargazerit.csv")
  stargaze_prep = read.csv("stargazerit.csv")
  stargazer(stargaze_prep, type=y, out=o, digits=2, median=TRUE)
}

#' Transform character string to start with uppercase letter
#' @seealso https://stackoverflow.com/questions/18509527/first-letter-to-upper-case
firstup <- function(x) {
  x <- tolower(x)
  substr(x, 1, 1) <- toupper(substr(x, 1, 1))
  x
}

#' Read multiple CSV with filename as column
read_csv_filename <- function(filename) {
  ret <- read.csv2(filename, stringsAsFactors=FALSE, sep=",")
  ret$source <- filename
  ret
}

#' Read all sheets from an Excel file
#' @seealso https://stackoverflow.com/questions/12945687/read-all-worksheets-in-an-excel-workbook-into-an-r-list-with-data-frames
read_excel_allsheets <- function(filename, fix_sheet1 = FALSE) {
  sheets <- readxl::excel_sheets(filename)
  if(fix_sheet1 & "Sheet1"%in%sheets)
    sheets <- sheets[!grepl("Sheet1",unlist(sheets))]
  x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X))
  names(x) <- sheets
  x
}
read_excel_allsheets_gdata <- function(filename, fix_sheet1 = FALSE) {
  sheets <- gdata::sheetNames(filename)
  if(fix_sheet1 & "Sheet1"%in%sheets)
    sheets <- sheets[!grepl("Sheet1",unlist(sheets))]
  x <- lapply(sheets, function(X) gdata::read.xls(filename, sheet = X))
  names(x) <- sheets
  x
}

read_multiple_excel <- function(path) {
  path %>%
    excel_sheets() %>% 
    set_names() %>% 
    map_df(read_excel, path = path)
}

#' Calculate mode of a vector
stats_mode <- function(v, ...) {
  return(v[which.max(tabulate(match(v, unique(v))))])
}

#' Normalize continous variable
normalize <- function(x) {
  if(!is.numeric(x)) {stop("Variable needs to be numeric to be normalized with this function")}
  
  if(all(x == 0)) {
    return(0)
  } else {
    return((x - min(x, na.rm=TRUE)) / (max(x, na.rm=TRUE) - min(x, na.rm=TRUE)))
  }
}

#' Find the N largest value in a vector x.
#' @author Zach 
#' @seealso https://stackoverflow.com/questions/2453326/fastest-way-to-find-second-third-highest-lowest-value-in-vector-or-column?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
max_n <- function(x, N=1){
  len <- length(x)
  if(N>len){
    warning('N greater than length(x).  Setting N=length(x)')
    N <- length(x)
  }
  sort(x,partial=len-N+1)[len-N+1]
}

#' Check if values of a vector are unique
isUnique <- function(vector){
  return(!any(duplicated(vector)))
}

